package controller;

import com.alibaba.fastjson.JSON;
import common.jsondata.IResponseJSONResult;
import common.jsondata.impl.ResponseJSONResultImpl;
import entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户登录controller控制
 */
@Controller
@RequestMapping(value="/login")
public class LoginController {

//    @Autowired
//    private UserServiceImpl userService;

    private Logger logger = LoggerFactory.getLogger(LoginController.class);

    //跳转登录页
    @RequestMapping("/tologin")
    public String tologin(){
        return  "views/user/login";
    }

    @RequestMapping(value = "/checkuser")
    //@ResponseBody
    public  String checkUser(HttpServletRequest request, HttpServletResponse response){
        IResponseJSONResult responseJSONResult = new ResponseJSONResultImpl();
        String username = request.getParameter("username");
        String password = request.getParameter("pass");

        logger.debug("用户名[ " +  username + " ]");
        logger.debug("密码[ " + password + " ]" );
        try {
            logger.info("开始验证账号和密码");
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
            Subject subject  = SecurityUtils.getSubject();
            subject.login(usernamePasswordToken);
            HttpSession session = request.getSession();
            session.setAttribute("username", username);
            logger.info("账号和密码验证结束");
            return  "redirect:/toindex/index";
        }catch (Exception e) {
            logger.error("Login Error:", e);
            return  responseJSONResult.ReturnJSONResunt(response, "账号或密码错误");
            //response.setStatus(ResponseVO.failCode);
            //Throwable ex = e.getCause();
//            if(ex instanceof BugException) {
//                if(ex.getMessage() != null) {
//                    response.setMessage(ex.getMessage());
//                }
//            }else if(e instanceof IncorrectCredentialsException) {
//                response.setMessage("密码错误");
//            }else {
//                response.setMessage("登录失败");
//            }
        }

        //1.验证账号是否存在
//        List<User> userList =  userService.findUserByID(username);
//        logger.debug("查询到的用户==> " + JSON.toJSONString(userList) );
//        if(userList.size() > 0){
//            if(!userList.get(0).getPassword().equals(password)){
//                logger.info("密码：" + password + "错误！");
//                return responseJSONResult.ReturnJSONResunt(response, "密码输入错误");
//            }
//        }else{
//            logger.info("用户 ：" + username + "不存在！" );
//            return responseJSONResult.ReturnJSONResunt(response, "用户名不存在");
//        }
//        return responseJSONResult.ReturnJSONResunt(response, "验证通过");
    }
}
